package mall.service;

import mall.pojo.ProductDO;
import mall.promotion.PromotionManager;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 结算服务类
 * @author zmy
 * @since 2025/6/27 23:29
 */
public class CheckoutService {
    private PromotionManager promotionManager;
    private ShelfService shelfService = ShelfService.getInstance();

    public CheckoutService(PromotionManager promotionManager) {
        this.promotionManager = promotionManager;
    }

    /**
     * 计算当前购买的价格信息
     * @param shopCar 购物车
     * @return double 最终应付价格
     */
    public double checkout(List<ProductDO> shopCar) {
        // 结算前的原价
        double originalTotalPrice = shopCar.stream()
                .mapToDouble(p -> p.getPrice() * p.getQuantity())
                .sum();
        // 满减的价格
        double reduction = promotionManager.applyPromotions(shopCar);
        // 折扣的优惠价格
        double discountTotalPrice = originalTotalPrice - shopCar.stream()
                .mapToDouble(p -> p.getPrice() * p.getQuantity())
                .sum();
        // 实付
        double finalTotal = originalTotalPrice - discountTotalPrice - reduction;

        System.out.println("原价金额：" + originalTotalPrice);
        System.out.println("满减优惠金额：" + reduction);
        System.out.println("折扣优惠金额：" + discountTotalPrice);
        System.out.println("实付金额：" + finalTotal);

        BigDecimal bd = BigDecimal.valueOf(finalTotal);
        return bd.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
}
